<template>
    <div class="app-container calendar-list-container" v-loading="loading" element-loading-text="正在导出数据">
    <!-- filter 条件查询 -->
    <div class="filter-container">
      <el-input
            clearable
            v-model="listQuery.directorScoreCode"
            :style="searchInputStyle"
            class="filter-item"
            placeholder="请输入代码"
      ></el-input>
      <!-- :style="searchInputStyle" -->
      <el-input
            clearable
            v-model="listQuery.directorDesc"
            style="margin-right: 15px"
            :style="{ width: inputWid(listQuery.directorDesc) }"
            class="filter-item"
            placeholder="请输入考评内容"
      ></el-input>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="getData">搜索</el-button>
    </div>

    <!-- table 表格 -->
    <el-table  :data="tableInfo" v-loading="loadingList" element-loading-text="给我一首歌的时间" stripe border fit highlight-current-row style="width: 100%">
      <el-table-column  label="序号" type="index" align="center" width="80px"/>
      <el-table-column label="代码"  prop="directorScoreCode"   align="center"/>
      <el-table-column label="考评内容"  prop="directorDesc"   align="center"/>
      <el-table-column  label="分值" prop='score' align="center" width="100px"/>
      <!-- 对工段绩效评分数据进行编辑与删除操作 -->
      <el-table-column  label="操作"  align="center" width="160">
        <template slot-scope="scope" >
              <el-button size="mini" type="primary" @click="handleEdit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
   
   <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[5,10, 20, 30, 40, 100, 300, 500]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-divider></el-divider>

    <el-button style="margin-right: 10px" type="success" size="small" @click="hExportExcel">导出数据Excel</el-button>

    <!-- 修改工段绩效评分信息弹出表单 -->
    <el-dialog title="工段绩效评分信息修改" :visible.sync="dialogFormVisible" width="700px">
    <el-form :model="modifyform">
      <el-form-item label="代码" :label-width="formLabelWidth">
        <el-input v-model="modifyform.directorScoreCode" :disabled="true" style="width:400px"/>
      </el-form-item>
      <el-form-item label="考评内容" :label-width="formLabelWidth">
        <el-input clearable v-model="modifyform.directorDesc" style="width:400px" />
      </el-form-item>
      <el-form-item label="分值" :label-width="formLabelWidth">
        <el-input clearable v-model="modifyform.score" style="width:400px" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="modify">确 定</el-button>
    </div>
    </el-dialog>

  </div>
</template>

<script>
import { findDirectorScore, updDirectorScore } from '../../api/evaluate'
export default {
    name: 'leader_evaluate',
    data () {
        return {
            listQuery: {
                directorScoreCode: '',                              //代码
                directorDesc: '',                              //考评内容
                page: 1,                                    //页码
                limit: 10                                   //每页多少条记录
            },
            modifyform: {
                directorScoreCode: '',                              //代码
                directorDesc: '',                              //考评内容
                score: ''                            //分值
            },
            formLabelWidth: '100px',
            searchInputStyle: 'width: 150px;margin-right:15px;',
            total: 0,                                      //数据的总数
            tableInfo: [],
            dialogFormVisible: false,
            loadingList: false,
            loading: false
        }
    },
    computed: {
      inputWid() {
        return function (value) {
            if(!value || (value.length <= 10)){
              return '170px'
            } else {
              return (170 + (String(value).length-10)*12) + 'px'
            }
        }
      }
    },
    methods: {
        getData(){
          this.listQuery.page = 1
          this.listQuery.limit = 10
          this.handleFilter()
        },
        async handleFilter() {
            this.loadingList = true
            let query = Object.assign({},this.listQuery)
            let {data} = await findDirectorScore(query)
            this.total = data.count
            this.tableInfo = data.list
            this.loadingList = false
        },
        handleEdit(item) {
            this.modifyform = Object.assign({},item)
            this.dialogFormVisible = true
        },
        // 将弹框修改后的工段绩效评分信息传入后台，并且消息提示是否修改成功
        async modify() {
            this.dialogFormVisible = false
            let query = Object.assign({},this.modifyform)
            const data = await updDirectorScore(query)
            if(data.code == '1'){
                this.$message({
                  message: '修改成功',
                  type: 'success'
              })
              this.handleFilter()
            } else {
              this.$message({
                  message: '修改失败',
                  type: 'warning'
              })
          }
        },
        // table表单下方的页码组件改变每页显示的数据条数
        handleSizeChange(val) {
          this.listQuery.limit = val
          this.handleFilter()
        },
        // table表单下方的页码组件改变页面
        handleCurrentChange(val) {
          this.listQuery.page = val
          this.handleFilter()
        },
        transformDat(res) {
          const mapInfo = {
            index: '序号',
            directorScoreCode: '代码',
            directorDesc: '考评内容',
            score: '分值',
          }
          // 对data进行格式化
          // 取出所有的属性名
          const props = Object.keys(res[0]) // ['id','']
          console.log(props)
          // 将属性名从英文转为中文
          const header = props.map(zhkey => mapInfo[zhkey])
          console.log('中文的表头', header)
          // const data=[['xxxx','总裁办'，'13800000']]
          const data = res.map(obj => Object.values(obj))
          console.log(data)
          return { header, data }
        },
        // 导出excel
        async hExportExcel() {
          this.loading = true
          let query = Object.assign({},this.listQuery)
          query.page = 1
          query.limit = 10000

          let arr = await findDirectorScore(query)
          const { header, data } = this.transformDat(this.setSort(arr.data.list))
            import('@/vendor/Export2Excel').then(excel => {
            // excel表示导入的模块对象
            console.log(excel)
            excel.export_json_to_excel({
            header, // 表头 必填
            data, // 具体数据 必填
            filename: '工段绩效评价excel', // 文件名称
            autoWidth: true, // 宽度是否自适应
            bookType: 'xlsx' // 生成的文件类型
            })
          })
          this.loading = false
        },
        // 排序函数
        setSort(arr){
          let newArr = JSON.parse(JSON.stringify(arr))
          let arr1 = []
          newArr.forEach((item,index)=>{
            let obj = {
              index: index+1,
              directorScoreCode: item.directorScoreCode,
              directorDesc: item.directorDesc,
              score: item.score
          }
          arr1.push(obj)
          })
          return arr1
        },
    },
    // 进入页面获取table信息
    mounted() {
        this.handleFilter()
    }
}
</script>

<style>

</style>